package com.yubest.demo.anno.handler;

import com.yubest.demo.anno.Login;
import com.yubest.demo.dto.LoginBean;
import com.yubest.demo.util.CacheUtil;
import org.springframework.util.StringUtils;

/**
 * 登陆处理器
 * @Author hweiyu
 * @Description
 * @Date 2021/3/23 10:57
 */
public class LoginHandler implements Handler {

    private final static String TOKEN = "access-token";

    @Override
    public void execute(Context context) {
        if (null == context.getMethod().getAnnotation(Login.class)) {
            return;
        }
        String token = context.getHeaders().get(TOKEN);
        if (!StringUtils.hasText(token)) {
            throw new RuntimeException("未登陆");
        }
        LoginBean login = CacheUtil.get(token, LoginBean.class);
        if (null == login) {
            if (StringUtils.hasText(token)) {
                throw new RuntimeException("未登陆");
            }
        }
    }
}
